package com.example.micro_property_springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.micro_property_springboot.pojo.AssetCheckPlan;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckPlanCreateRequest;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCheckPlanUpdateRequest;
import com.example.micro_property_springboot.pojo.VO.PageResult;
import com.example.micro_property_springboot.pojo.VO.Result;
import com.example.micro_property_springboot.service.AssetCheckPlanService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
@RestController
@RequestMapping("/check-plan")
@Tag(name = "盘点计划管理", description = "资产盘点计划相关操作")
public class AssetCheckPlanController {

    @Autowired
    private AssetCheckPlanService assetCheckPlanService;

    @PostMapping
    @Operation(summary = "创建盘点计划", description = "创建新的资产盘点计划")
    public Result<AssetCheckPlan> createAssetCheckPlan(@Valid @RequestBody AssetCheckPlanCreateRequest request) {
        AssetCheckPlan plan = assetCheckPlanService.createAssetCheckPlan(request);
        return Result.success("创建成功", plan);
    }

    @GetMapping
    @Operation(summary = "分页查询盘点计划列表", description = "根据状态和检查用户ID分页查询资产盘点计划列表")
    public Result<PageResult<AssetCheckPlan>> getAssetCheckPlans(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String planNo,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Long checkUserId) {

        // 设置分页参数
        Page<AssetCheckPlan> page = new Page<>(pageNum, pageSize);

        // 设置条件查询
        QueryWrapper<AssetCheckPlan> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(planNo), "plan_no", planNo)
                .eq(StringUtils.isNotBlank(status), "status", status)
                .eq(checkUserId != null, "check_user_id", checkUserId);

        // 执行查询
        Page<AssetCheckPlan> planPage = assetCheckPlanService.page(page, wrapper);

        // 转换为自定义的PageResult
        PageResult<AssetCheckPlan> pageResult = new PageResult<>();
        pageResult.setTotal(planPage.getTotal());
        pageResult.setRows(planPage.getRecords());

        return Result.success("查询成功", pageResult);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取盘点计划详情", description = "根据盘点计划ID获取资产盘点计划详情")
    public Result<AssetCheckPlan> getAssetCheckPlanById(@PathVariable Long id) {
        AssetCheckPlan plan = assetCheckPlanService.getById(id);
        return plan != null ? Result.success("查询成功", plan) : Result.error("盘点计划不存在");
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新盘点计划", description = "根据盘点计划ID更新资产盘点计划")
    public Result<AssetCheckPlan> updateAssetCheckPlan(
            @PathVariable Long id,
            @Valid @RequestBody AssetCheckPlanUpdateRequest request) {
        AssetCheckPlan plan = assetCheckPlanService.updateAssetCheckPlan(id, request);
        return plan != null ? Result.success("更新成功", plan) : Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除盘点计划", description = "根据盘点计划ID删除资产盘点计划")
    public Result<Void> deleteAssetCheckPlan(@PathVariable Long id) {
        boolean deleted = assetCheckPlanService.removeById(id);
        return deleted ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("/{id}/start")
    @Operation(summary = "开始盘点", description = "根据盘点计划ID开始资产盘点")
    public Result<AssetCheckPlan> startAssetCheckPlan(@PathVariable Long id) {
        AssetCheckPlan plan = assetCheckPlanService.startAssetCheckPlan(id);
        return plan != null ? Result.success("开始盘点成功", plan) : Result.error("开始盘点失败");
    }

    @PostMapping("/{id}/complete")
    @Operation(summary = "完成盘点", description = "根据盘点计划ID完成资产盘点")
    public Result<AssetCheckPlan> completeAssetCheckPlan(@PathVariable Long id) {
        AssetCheckPlan plan = assetCheckPlanService.completeAssetCheckPlan(id);
        return plan != null ? Result.success("完成盘点成功", plan) : Result.error("完成盘点失败");
    }
    /**
     * 取消盘点计划
     * @param id 盘点计划ID
     * @return 取消结果
     */
    @PostMapping("/{id}/cancel")
    @Operation(summary = "取消盘点计划", description = "根据盘点计划ID取消资产盘点计划")
    public Result<AssetCheckPlan> cancelAssetCheckPlan(@PathVariable Long id) {
        AssetCheckPlan plan = assetCheckPlanService.cancelAssetCheckPlan(id);
        return plan != null ? Result.success("取消盘点计划成功", plan) : Result.error("取消盘点计划失败");
    }
}

